import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/a_elder/models/elder_entity.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/leave_record_list.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/ui/leave/add_leave_page.dart';

class LeaveRecordViewModel extends BaseListViewModel<LeaveRecordList> {

  final ElderInfo? info;
  LeaveRecordViewModel(BuildContext context,{this.info}) : super(context){
    if(info!=null){
      customerNameLike= info?.name??"";
    }
  }

  var customerNameLike = "";
  var validSign = "";
  var validSignName = "全部";
  var leaveTimeStart = "";
  var leaveTimeEnd = "";
  var backTimeStart = "";
  var backTimeEnd = "";
  var accompanyNameLike = "";
  var accompanyMobile = "";

  @override
  Future<List<LeaveRecordList>> requestListData() async {
    var data = await PostRequest.leaveRecordList(
        customerNameLike,
        validSign,
        startDateStr,
        endDateStr,
        backTimeStart,
        backTimeEnd,
        accompanyNameLike,
        accompanyMobile);

    if ("0" == data['code']) {
      return ((data['data']['rows'] as List?)??[]).map((e) => LeaveRecordList.fromJson(e)).toList();
    } else {
      throw(data['msg']);
    }
  }

  void toAddSign(){
    Go().push(AddLeavePage(info: info,));
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }


  DateTime? backStartDateTime;
  DateTime? backEndDateTime;

  void selectBackStartDate({
    DateTime? min,
    DateTime? max,
    String title = "",
    List<String> formats = const [yyyy,'-',mm,'-',dd],
    Function(String date)? click
  }) {
    DatePicker.showDatePicker(context,
        currentTime: startDateTime,
        minTime: min,
        maxTime: max,
        title: title,
        onConfirm: (date) {
          if(backEndDateTime!=null){
            if(date!.isAfter(backEndDateTime!)){
              showToast('开始时间不可晚于结束时间');
              return;
            }
          }
          backStartDateTime = date;
          backTimeStart = backStartDateTime?.toDateStr(formats)??"";
          notifyListeners();
          if(click!=null){
            click(backTimeStart);
          }
        });
  }

  void selectBackEndDate({
    DateTime? min,
    DateTime? max,
    String title = "",
    List<String> formats = const [yyyy,'-',mm,'-',dd],
    Function(String date)? click
  }) {
    DatePicker.showDatePicker(context,
        currentTime: endDateTime,
        minTime: min,
        maxTime: max,
        title: title,
        onConfirm: (date) {
          if(backStartDateTime!=null){
            if(date!.isBefore(backStartDateTime!)){
              showToast('结束时间不可早于开始时间');
              return;
            }
          }
          backEndDateTime = date;
          backTimeEnd = backEndDateTime!.toDateStr(formats);
          notifyListeners();
          if(click!=null){
            click(backTimeEnd);
          }
        });
  }

  void selectStatus(){
    var items = [PickerDataWithIdModel(pickName: "全部",pickId: ""),
      PickerDataWithIdModel(pickName: "已离院",pickId: "1"),
      PickerDataWithIdModel(pickName: "已回院",pickId: "2"),
    ];
    DataPickerView(context, items,onSubmitListener: (p1,p2,p3){
      var item = items[p1];
      validSign = item.pickId??"";
      validSignName = item.pickName??"";
      notifyListeners();
      requestRefresh();
    }).show();
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if(event.msgCode==MessageConfig.FINISH_LEAVE_SIGN){
      requestRefresh();
    }
  }
}
